<%@ CodePage="65001" LCID="2052" %>
<%
Response.Buffer = EW_RESPONSE_BUFFER
%>
<!--#include file="lc_ewcfg8.asp"-->
<!--#include file="lc_sys_company_catinfo.asp"-->
<!--#include file="lc_userinfo.asp"-->
<!--#include file="lc_aspfn8.asp"-->
<!--#include file="md5.asp"-->
<!--#include file="lc_userfn8.asp"-->
<% Session.Timeout = 20 %>
<%
Response.Expires = 0
Response.ExpiresAbsolute = Now() - 1
Response.AddHeader "pragma", "no-cache"
Response.AddHeader "cache-control", "private, no-cache, no-store, must-revalidate"
%>
<%

' Define page object
Dim sys_company_cat_addopt
Set sys_company_cat_addopt = New csys_company_cat_addopt
Set Page = sys_company_cat_addopt

' Page init processing
Call sys_company_cat_addopt.Page_Init()

' Page main processing
Call sys_company_cat_addopt.Page_Main()
%>
<script type="text/javascript">
<!--
var sys_company_cat_addopt = new ew_Page("sys_company_cat_addopt");
// page properties
sys_company_cat_addopt.PageID = "addopt"; // page ID
sys_company_cat_addopt.FormID = "fsys_company_cataddopt"; // form ID
var EW_PAGE_ID = sys_company_cat_addopt.PageID; // for backward compatibility
// extend page with ValidateForm function
sys_company_cat_addopt.ValidateForm = function(fobj) {
	return true; // ignore validation
}
//-->
</script>
<% If EW_DEBUG_ENABLED Then Response.Write sys_company_cat_addopt.DebugMsg %>
<% sys_company_cat_addopt.ShowMessage %>
<form name="fsys_company_cataddopt" id="fsys_company_cataddopt" action="lc_sys_company_cataddopt.asp" method="post" onsubmit="return sys_company_cat_addopt.ValidateForm(this);">
<p>
<input type="hidden" name="t" id="t" value="sys_company_cat">
<input type="hidden" name="a_addopt" id="a_addopt" value="A">
<table class="ewTableAddOpt">
	<tr>
		<td><%= sys_company_cat.sys_comp_name.FldCaption %></td>
		<td><span id="el_sys_comp_name">
<input type="text" name="x_sys_comp_name" id="x_sys_comp_name" title="<%= sys_company_cat.sys_comp_name.FldTitle %>" size="30" maxlength="50" value="<%= sys_company_cat.sys_comp_name.EditValue %>"<%= sys_company_cat.sys_comp_name.EditAttributes %>>
</span></td>
	</tr>
	<tr>
		<td><%= sys_company_cat.sys_comp_parent_id.FldCaption %></td>
		<td><span id="el_sys_comp_parent_id">
<input type="text" name="x_sys_comp_parent_id" id="x_sys_comp_parent_id" title="<%= sys_company_cat.sys_comp_parent_id.FldTitle %>" size="30" value="<%= sys_company_cat.sys_comp_parent_id.EditValue %>"<%= sys_company_cat.sys_comp_parent_id.EditAttributes %>>
</span></td>
	</tr>
</table>
<p>
</form>
<%

' Drop page object
Set sys_company_cat_addopt = Nothing
%>
<%

' -----------------------------------------------------------------
' Page Class
'
Class csys_company_cat_addopt

	' Page ID
	Public Property Get PageID()
		PageID = "addopt"
	End Property

	' Table Name
	Public Property Get TableName()
		TableName = "sys_company_cat"
	End Property

	' Page Object Name
	Public Property Get PageObjName()
		PageObjName = "sys_company_cat_addopt"
	End Property

	' Page Name
	Public Property Get PageName()
		PageName = ew_CurrentPage()
	End Property

	' Page Url
	Public Property Get PageUrl()
		PageUrl = ew_CurrentPage() & "?"
		If sys_company_cat.UseTokenInUrl Then PageUrl = PageUrl & "t=" & sys_company_cat.TableVar & "&" ' add page token
	End Property

	' Common urls
	Dim AddUrl
	Dim EditUrl
	Dim CopyUrl
	Dim DeleteUrl
	Dim ViewUrl
	Dim ListUrl

	' Export urls
	Dim ExportPrintUrl
	Dim ExportHtmlUrl
	Dim ExportExcelUrl
	Dim ExportWordUrl
	Dim ExportXmlUrl
	Dim ExportCsvUrl

	' Inline urls
	Dim InlineAddUrl
	Dim InlineCopyUrl
	Dim InlineEditUrl
	Dim GridAddUrl
	Dim GridEditUrl
	Dim MultiDeleteUrl
	Dim MultiUpdateUrl

	' Debug Message
	Dim m_DebugMsg

	Public Property Get DebugMsg()
		If m_DebugMsg <> "" Then
			DebugMsg = "<p>" & m_DebugMsg & "</p>"
		Else
			DebugMsg = ""
		End If
	End Property

	Public Property Let DebugMsg(v)
		If m_DebugMsg <> "" Then ' Append
			m_DebugMsg = m_DebugMsg & "<br>" & v
		Else
			m_DebugMsg = v
		End If
	End Property

	' Message
	Public Property Get Message()
		Message = Session(EW_SESSION_MESSAGE)
	End Property

	Public Property Let Message(v)
		If Session(EW_SESSION_MESSAGE) <> "" Then ' Append
			Session(EW_SESSION_MESSAGE) = Session(EW_SESSION_MESSAGE) & "<br>" & v
		Else
			Session(EW_SESSION_MESSAGE) = v
		End If
	End Property

	' Show Message
	Public Sub ShowMessage()
		Dim sMessage
		sMessage = Message
		Call Message_Showing(sMessage)
		If sMessage <> "" Then Response.Write "<p><span class=""ewMessage"">" & sMessage & "</span></p>"
		Session(EW_SESSION_MESSAGE) = "" ' Clear message in Session
	End Sub

	' -----------------------
	'  Validate Page request
	'
	Public Function IsPageRequest()
		If sys_company_cat.UseTokenInUrl Then
			IsPageRequest = False
			If Not (ObjForm Is Nothing) Then
				IsPageRequest = (sys_company_cat.TableVar = ObjForm.GetValue("t"))
			End If
			If Request.QueryString("t").Count > 0 Then
				IsPageRequest = (sys_company_cat.TableVar = Request.QueryString("t"))
			End If
		Else
			IsPageRequest = True
		End If
	End Function

	' -----------------------------------------------------------------
	'  Class initialize
	'  - init objects
	'  - open ADO connection
	'
	Private Sub Class_Initialize()
		StartTimer = Timer ' Init start time

		' Initialize language object
		Set Language = New cLanguage

		' Initialize table object
		Set sys_company_cat = New csys_company_cat

		' Initialize urls
		' Initialize other table object

		Set user = New cuser

		' Initialize form object
		Set ObjForm = Nothing

		' Intialize page id (for backward compatibility)
		EW_PAGE_ID = "addopt"

		' Initialize table name (for backward compatibility)
		EW_TABLE_NAME = "sys_company_cat"

		' Open connection to the database
		Call ew_Connect()
	End Sub

	' -----------------------------------------------------------------
	'  Subroutine Page_Init
	'  - called before page main
	'  - check Security
	'  - set up response header
	'  - call page load events
	'
	Sub Page_Init()
		Set Security = New cAdvancedSecurity
		If Not Security.IsLoggedIn() Then Call Security.AutoLogin()
		If Not Security.IsLoggedIn() Then
			Call Security.SaveLastUrl()
			Call Page_Terminate("lc_login.asp")
		End If

	' Create form object
	Set ObjForm = New cFormObj

		' Global page loading event (in userfn7.asp)
		Call Page_Loading()

		' Page load event, used in current page
		Call Page_Load()
	End Sub

	' -----------------------------------------------------------------
	'  Class terminate
	'  - clean up page object
	'
	Private Sub Class_Terminate()
		Call Page_Terminate("")
	End Sub

	' -----------------------------------------------------------------
	'  Subroutine Page_Terminate
	'  - called when exit page
	'  - clean up ADO connection and objects
	'  - if url specified, redirect to url
	'
	Sub Page_Terminate(url)

		' Page unload event, used in current page
		Call Page_Unload()

		' Global page unloaded event (in userfn60.asp)
		Call Page_Unloaded()
		If Not (Conn Is Nothing) Then Conn.Close ' Close Connection
		Set Conn = Nothing
		Set Security = Nothing
		Set sys_company_cat = Nothing
		Set ObjForm = Nothing

		' Go to url if specified
		Dim sRedirectUrl
		sReDirectUrl = url
		Call Page_Redirecting(sReDirectUrl)
		If sReDirectUrl <> "" Then
			If Response.Buffer Then Response.Clear
			Response.Redirect sReDirectUrl
		End If
	End Sub

	'
	'  Subroutine Page_Terminate (End)
	' ----------------------------------------
	' -----------------------------------------------------------------
	' Page main processing
	'
	Sub Page_Main()

		' Process form if post back
		If ObjForm.GetValue("a_addopt")&"" <> "" Then
			sys_company_cat.CurrentAction = ObjForm.GetValue("a_addopt") ' Get form action
			Call LoadFormValues() ' Load form values

			' Validate Form
			If Not ValidateForm() Then
				sys_company_cat.CurrentAction = "I" ' Form error, reset action
				Message = gsFormError
			End If

		' Not post back
		Else
			sys_company_cat.CurrentAction = "I" ' Display Blank Record
			Call LoadDefaultValues() ' Load default values
		End If

		' Perform action based on action code
		Select Case sys_company_cat.CurrentAction
			Case "I" ' Blank record, no action required
			Case "A" ' Add new record
				sys_company_cat.SendEmail = True ' Send email on add success
				If AddRow() Then ' Add successful
					Dim XMLDoc
					Set XMLDoc = New cXMLDocument
					Call XMLDoc.AddRowEx("result")
					Call XMLDoc.AddField("x_sys_comp_id", sys_company_cat.sys_comp_id.DbValue)
					Call XMLDoc.AddField("x_sys_comp_name", sys_company_cat.sys_comp_name.FormValue)
					Call XMLDoc.AddField("x_sys_comp_parent_id", sys_company_cat.sys_comp_parent_id.FormValue)
					XMLDoc.Encoding = "utf-8"
					XMLDoc.Output
					Set XMLDoc = Nothing
					Call Page_Terminate("")
					Response.End
				Else
					Call RestoreFormValues() ' Add failed, restore form values
				End If
		End Select

		' Render row
		sys_company_cat.RowType = EW_ROWTYPE_ADD ' Render add type
		Call RenderRow()
	End Sub

	' -----------------------------------------------------------------
	' Function Get upload files
	'
	Function GetUploadFiles()

		' Get upload data
	End Function

	' -----------------------------------------------------------------
	' Load default values
	'
	Function LoadDefaultValues()
		sys_company_cat.sys_comp_parent_id.CurrentValue = 0
	End Function

	' -----------------------------------------------------------------
	' Load form values
	'
	Function LoadFormValues()

		' Load values from form
		sys_company_cat.sys_comp_name.FormValue = ObjForm.GetValue("x_sys_comp_name")
		sys_company_cat.sys_comp_name.OldValue = ObjForm.GetValue("o_sys_comp_name")
		sys_company_cat.sys_comp_parent_id.FormValue = ObjForm.GetValue("x_sys_comp_parent_id")
		sys_company_cat.sys_comp_parent_id.OldValue = ObjForm.GetValue("o_sys_comp_parent_id")
		sys_company_cat.sys_comp_id.FormValue = ObjForm.GetValue("x_sys_comp_id")
	End Function

	' -----------------------------------------------------------------
	' Restore form values
	'
	Function RestoreFormValues()
		sys_company_cat.sys_comp_name.CurrentValue = sys_company_cat.sys_comp_name.FormValue
		sys_company_cat.sys_comp_parent_id.CurrentValue = sys_company_cat.sys_comp_parent_id.FormValue
		sys_company_cat.sys_comp_id.CurrentValue = sys_company_cat.sys_comp_id.FormValue
	End Function

	' -----------------------------------------------------------------
	' Load row based on key values
	'
	Function LoadRow()
		Dim RsRow, sSql, sFilter
		sFilter = sys_company_cat.KeyFilter

		' Call Row Selecting event
		Call sys_company_cat.Row_Selecting(sFilter)

		' Load sql based on filter
		sys_company_cat.CurrentFilter = sFilter
		sSql = sys_company_cat.SQL
		If EW_DEBUG_ENABLED Then DebugMsg = "LoadRow: " & sSql ' Show SQL for debugging
		Set RsRow = ew_LoadRow(sSql)
		If RsRow.Eof Then
			LoadRow = False
		Else
			LoadRow = True
			RsRow.MoveFirst
			Call LoadRowValues(RsRow) ' Load row values

			' Call Row Selected event
			Call sys_company_cat.Row_Selected(RsRow)
		End If
		RsRow.Close
		Set RsRow = Nothing
	End Function

	' -----------------------------------------------------------------
	' Load row values from recordset
	'
	Sub LoadRowValues(RsRow)
		Dim sDetailFilter
		sys_company_cat.sys_comp_id.DbValue = RsRow("sys_comp_id")
		sys_company_cat.sys_comp_name.DbValue = RsRow("sys_comp_name")
		sys_company_cat.sys_comp_parent_id.DbValue = RsRow("sys_comp_parent_id")
	End Sub

	' -----------------------------------------------------------------
	' Render row values based on field settings
	'
	Sub RenderRow()

		' Initialize urls
		' Call Row Rendering event

		Call sys_company_cat.Row_Rendering()

		' ---------------------------------------
		'  Common render codes for all row types
		' ---------------------------------------
		' sys_comp_name

		sys_company_cat.sys_comp_name.CellCssStyle = ""
		sys_company_cat.sys_comp_name.CellCssClass = ""

		' sys_comp_parent_id
		sys_company_cat.sys_comp_parent_id.CellCssStyle = ""
		sys_company_cat.sys_comp_parent_id.CellCssClass = ""

		' -----------
		'  View  Row
		' -----------

		If sys_company_cat.RowType = EW_ROWTYPE_VIEW Then ' View row

			' sys_comp_id
			sys_company_cat.sys_comp_id.ViewValue = sys_company_cat.sys_comp_id.CurrentValue
			sys_company_cat.sys_comp_id.CssStyle = ""
			sys_company_cat.sys_comp_id.CssClass = ""
			sys_company_cat.sys_comp_id.ViewCustomAttributes = ""

			' sys_comp_name
			sys_company_cat.sys_comp_name.ViewValue = sys_company_cat.sys_comp_name.CurrentValue
			sys_company_cat.sys_comp_name.CssStyle = ""
			sys_company_cat.sys_comp_name.CssClass = ""
			sys_company_cat.sys_comp_name.ViewCustomAttributes = ""

			' sys_comp_parent_id
			sys_company_cat.sys_comp_parent_id.ViewValue = sys_company_cat.sys_comp_parent_id.CurrentValue
			sys_company_cat.sys_comp_parent_id.CssStyle = ""
			sys_company_cat.sys_comp_parent_id.CssClass = ""
			sys_company_cat.sys_comp_parent_id.ViewCustomAttributes = ""

			' View refer script
			' sys_comp_name

			sys_company_cat.sys_comp_name.HrefValue = ""
			sys_company_cat.sys_comp_name.TooltipValue = ""

			' sys_comp_parent_id
			sys_company_cat.sys_comp_parent_id.HrefValue = ""
			sys_company_cat.sys_comp_parent_id.TooltipValue = ""

		' ---------
		'  Add Row
		' ---------

		ElseIf sys_company_cat.RowType = EW_ROWTYPE_ADD Then ' Add row

			' sys_comp_name
			sys_company_cat.sys_comp_name.EditCustomAttributes = ""
			sys_company_cat.sys_comp_name.EditValue = ew_HtmlEncode(sys_company_cat.sys_comp_name.CurrentValue)

			' sys_comp_parent_id
			sys_company_cat.sys_comp_parent_id.EditCustomAttributes = ""
			sys_company_cat.sys_comp_parent_id.EditValue = ew_HtmlEncode(sys_company_cat.sys_comp_parent_id.CurrentValue)
		End If

		' Call Row Rendered event
		Call sys_company_cat.Row_Rendered()
	End Sub

	' -----------------------------------------------------------------
	' Validate form
	'
	Function ValidateForm()

		' Initialize
		gsFormError = ""
		If Not ew_CheckInteger(sys_company_cat.sys_comp_parent_id.FormValue) Then
			If gsFormError <> "" Then gsFormError = gsFormError & "<br>"
			gsFormError = gsFormError & sys_company_cat.sys_comp_parent_id.FldErrMsg
		End If

		' Return validate result
		ValidateForm = (gsFormError = "")

		' Call Form Custom Validate event
		Dim sFormCustomError
		sFormCustomError = ""
		ValidateForm = ValidateForm And Form_CustomValidate(sFormCustomError)
		If sFormCustomError <> "" Then
			If gsFormError <> "" Then gsFormError = gsFormError & "<br>"
			gsFormError = gsFormError & sFormCustomError
		End If
	End Function

	' -----------------------------------------------------------------
	' Add record
	'
	Function AddRow()
		If Not EW_DEBUG_ENABLED Then On Error Resume Next
		Dim Rs, sSql, sFilter
		Dim RsNew
		Dim bInsertRow
		Dim RsChk
		Dim sIdxErrMsg

		' Clear any previous errors
		Err.Clear

		' Add new record
		sFilter = "(0 = 1)"
		sys_company_cat.CurrentFilter = sFilter
		sSql = sys_company_cat.SQL
		Set Rs = Server.CreateObject("ADODB.Recordset")
		Rs.CursorLocation = EW_CURSORLOCATION
		Rs.Open sSql, Conn, 1, 2
		Rs.AddNew
		If Err.Number <> 0 Then
			Message = Err.Description
			Rs.Close
			Set Rs = Nothing
			AddRow = False
			Exit Function
		End If

		' Field sys_comp_name
		Call sys_company_cat.sys_comp_name.SetDbValue(Rs, sys_company_cat.sys_comp_name.CurrentValue, Null, False)

		' Field sys_comp_parent_id
		Call sys_company_cat.sys_comp_parent_id.SetDbValue(Rs, sys_company_cat.sys_comp_parent_id.CurrentValue, Null, True)

		' Check recordset update error
		If Err.Number <> 0 Then
			Message = Err.Description
			Rs.Close
			Set Rs = Nothing
			AddRow = False
			Exit Function
		End If

		' Call Row Inserting event
		bInsertRow = sys_company_cat.Row_Inserting(Rs)
		If bInsertRow Then

			' Clone new recordset object
			Set RsNew = ew_CloneRs(Rs)
			Rs.Update
			If Err.Number <> 0 Then
				Message = Err.Description
				AddRow = False
			Else
				AddRow = True
			End If
		Else
			Rs.CancelUpdate
			If sys_company_cat.CancelMessage <> "" Then
				Message = sys_company_cat.CancelMessage
				sys_company_cat.CancelMessage = ""
			Else
				Message = Language.Phrase("InsertCancelled")
			End If
			AddRow = False
		End If
		Rs.Close
		Set Rs = Nothing
		If AddRow Then
			sys_company_cat.sys_comp_id.DbValue = RsNew("sys_comp_id")

			' Call Row Inserted event
			Call sys_company_cat.Row_Inserted(RsNew)
		End If
		If IsObject(RsNew) Then
			RsNew.Close
			Set RsNew = Nothing
		End If
	End Function

	' Page Load event
	Sub Page_Load()

		'Response.Write "Page Load"
	End Sub

	' Page Unload event
	Sub Page_Unload()

		'Response.Write "Page Unload"
	End Sub

	' Page Redirecting event
	Sub Page_Redirecting(url)

		'url = newurl
	End Sub

	' Message Showing event
	Sub Message_Showing(msg)

		'msg = newmsg
	End Sub

	' Form Custom Validate event
	Function Form_CustomValidate(CustomError)

		'Return error message in CustomError
		Form_CustomValidate = True
	End Function
End Class
%>
